<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] - ".__("Member Genealogy");
$page_title=__("Member Genealogy");

if(!$pv["task"]["Member Genealogy"]){
	$errmsg=format__rr(__("You do not have the privilege to access member genealogy."));

	print format_admin_page($errmsg, $this_title);
	exit();
}

require_once('inc.genealogy_tab.php');

$this_month['start'] = date('Y-m-d H:i:s', mktime(0,0,0,ndate('n'),1,ndate('Y')));
$this_month['end'] = date('Y-m-d H:i:s', mktime(23,59,59,ndate('n')+1,0,ndate('Y')));
$last_month['start'] = date('Y-m-d H:i:s', mktime(0,0,0,ndate('n')-1,1,ndate('Y')));
$last_month['end'] = date('Y-m-d H:i:s', mktime(23,59,59,ndate('n'),0,ndate('Y')));
$l_month = date('n', strtotime($last_month['start']));
$l_year = date('Y', strtotime($last_month['start']));

$r_status=array("normal","suspended","terminated");
$r_status_d=array("normal"=>__("Normal"),"suspended"=>__("Suspended"),"terminated"=>__("Terminated"));

//#####AJAX CALL#####
//ajax search user
if($_GET["aj"] && $_GET["search_user"]){
	$ajresult = ajax_search_user($post_d['search_user'], 'select_user', true, null, $pv['task']['View Company Account']);

	print format_xml(array('status'=>$ajresult['status'], 'msg'=>$ajresult['possible_users']));
	exit;
}
//downline child request
if($get_s['aj'] && $_GET["gen_tree"]){
	$user = get_user_detail_by_id($get_d['id']);
	if($user){
		$downline_node = retrieve_gen_downline_name($user['id'], 'ref', 'gen_tree', 2);
	}
	print $downline_node;
	exit;
}
//#####END AJAX CALL#####

//#####GET
if($_GET["uid"]){
	//#####GET DOWNLINE
	if(!$r_upline=get_user_detail_by_id($get_d['uid'])){
		$errmsg=format_err(__("No member with that ID found!"));
	}else{
		$upline_id=$r_upline["id"];
		
		$which_network = 'ref';
		$downline_node = retrieve_gen_downline_name($upline_id, 'ref', 'gen_tree', 2);
		
		$head = $r_upline;
		if($head["acc_type"]=="c"){
			$display_pic=JS_URL."/jqtree/images/node{$head[acc_type]}.gif";
		}elseif($head["gender"]=="m"){
			$display_pic=JS_URL."/jqtree/images/node{$head[gender]}.gif";
		}else{
			$display_pic=JS_URL."/tree/images/nodef.gif";
		}
		//ps
		$this_month_sales_maintain = calc_personal_sales($head['id'], $this_month['start'], $this_month['end']);
		$head_node = "<span class='gen_img'><img src='$display_pic' /></span> <span class='gen_id'>".$head["id"]."</span><span class='gen_id'>".$head["code"]."</span><span class='gen_name'>".$head["name"]."</span> ".
		"<span class='gen_jdate'>Joined ".date($vars['date_format'], strtotime($head["cdate"]))."</span>".
		" <span class='gen_pspgs'>Sales + Maintain: ".number_format($this_month_sales_maintain, 2)."</span>";
		$downline_tree="
		<div id='genealogy_tree' class='tree' style='width:auto;'>
		 <ul>
		  <li class='root' id='0'><div class='tree-node".(USER_BROWSER == 'msie'? "-ie" : '')."'>$head_node</div><div class='clear'></div>
		   <ul>
			$downline_node
		   </ul>
		  </li>
		 </ul>
		</div>";
		
		$downline_box="
		<p class='bold'>".replace_tag(__("Member Genealogy for User ID: <%uid%>, Name: <%name%>."), array("<%uid%>"=>$r_upline['id'], "<%name%>"=>$r_upline['name']))."</p>
		<div class='genealogy' style='width:auto;'>
			$downline_tree
		</div>";
		
		ob_start();
		?>
		<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,jquery.livequery.js,jqtree/tree.js"); ?>'></script>
		<script type='text/javascript'>
		jQuery(document).ready(function(j){
			j('#genealogy_tree').SimpleTree({animate: true});
		});
		</script>
		<link rel='stylesheet' href='<?php echo JS_URL; ?>/jqtree/style.css' type='text/css'>
		<?php
		$jvscript = ob_get_clean();
	}
}

$view_genealogy=($errmsg? "
$errmsg" : "")."
<form name='user_form' method='get' action='$this_file'>
<p>".__("To view a member genealogy, key in the User ID and click 'View Genealogy'.")."<br />\n
<input type='text' name='uid' value=\"$get_h[uid]\" />
<input type='submit' value=\"".__("View Genealogy")."\" />
</p>
<%member_search%>
</form>".($downline_box? "
$downline_box" : "");

//javascript
ob_start();
if(!$jvscript){
?>
<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,jquery.livequery.js"); ?>'></script>
<?php } ?>
<script type='text/javascript'>
jQuery(document).ready(function(j){
	j('input[@name=search_user]').keypress(function(e){
		if (e.which == 13) {
			j('input#search_user_btn').click();
			return false;
		}
	});
	
	j('input#search_user_btn').click(function(){
		j('div#search_user_result').html('').removeClass();
		if(j('input[@name=search_user]').val()==''){
			j('div#search_user_result').html('Please provide a member name / IC. to search.').addClass('red');
		}else{
			j(this).attr('disabled','disabled');
			j.ajax({
				url: '<?php echo "$this_file?aj=1&{$_SERVER['QUERY_STRING']}&search_user=1"; ?>',
				data: {'search_user': j('input[@name=search_user]').val()},
				type: 'post',
				dataType: 'xml',
				error: function(){
					j('div#search_user_result').html('Error checking...').addClass('red');
				},
				success: function(data){
					var mesg='';
					if(j('loggedout', data).text()=='loggedout'){
						mesg='You have been logged out.';
					}else{
						j(data).find('msg').each(function(){
							mesg+=j(this).text()+'<br />';
						});
					}
					var status=j(data).find('status').text();
					j('div#search_user_result').html(mesg).addClass(status==1? 'bold' : 'red');
				},
				complete: function(){
					j('input#search_user_btn').attr('disabled','');
				}
			});
		}
	});

	j('a[@name=select_user]').livequery('click', function(){
		var uid = j(this).attr('rel');
		j('input[@name=uid]').val(uid);
		return false;
	});
});
</script>
<?php
$jvscript2 = ob_get_clean();

$member_search="
<div style='width:600px; margin-bottom:10px;'>
	<div class='mini-search'>
		Search User: <input type='text' name='search_user' value=\"$get_h[search_user]\" /> <input type='button' id='search_user_btn' value='Search User' />
		<div id='search_user_result'></div>
	</div>
</div>";
$content = str_replace("<%member_search%>", $member_search, $view_genealogy);
$content="<h2>$page_title</h2>".str_replace("<%tab_content%>", $content, $tab);

print format_admin_page($content, $this_title, $css.$jvscript.$jvscript2);
?>